//
//  HomeViewController.m
//  恒记
//
//  Created by can on 16/5/5.
//  Copyright © 2016年 can. All rights reserved.
//

#import "HomeViewController.h"
#import "ControllorManager.h"
#import "HeadView.h"
#import "Home_cellTableViewCell.h"
#import "ColletionViewController.h"
#import "BottomView.h"

#define ruler (SCREEN_HEIGHT - CGRectGetMaxY(self.headView.frame) - 70 * PROPORTION_H) / 5
@interface HomeViewController ()<UITableViewDataSource, UITableViewDelegate>
{
    NSArray *_dataArray;
}
@property (nonatomic, strong) UIScrollView * scrollView;
@property (nonatomic, strong) HeadView * headView;
@property (nonatomic, strong) UIButton * recordButton;/**< 记账按钮 */
@property (nonatomic, strong) UITableView * tableView;
@property (nonatomic, strong) UIButton * drawerButton;/**< 抽屉 */
@property (nonatomic, strong) UIImageView * backView;
@property (nonatomic, strong) UIButton * moreButton;
@property (nonatomic, strong) UIVisualEffectView *visuaView;  /**< 模糊视图 */
@property (nonatomic, strong) BottomView * bottomView;
@property (nonatomic, strong) UIButton * chartButton;/**< 图表按钮 */
@property (nonatomic, strong) UIButton * detailButton;/**< 详情按钮 */

- (void)initializeDataSource;/**< 初始化数据源 */
- (void)initializeUserInterface;/**< 初始化用户界面 */
@end

@implementation HomeViewController

- (void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeDataSource];
    [self initializeUserInterface];
    
    
}
#pragma mark *** initialize Mothods ***
- (void)initializeDataSource{
    NSString *plist = [[NSBundle mainBundle] pathForAuxiliaryExecutable:@"Dateidentify.plist"];
    _dataArray = [NSArray arrayWithContentsOfFile:plist];
}

- (void)initializeUserInterface{
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.scrollView];
    [self.scrollView addSubview:self.headView];
    [self.scrollView addSubview:self.recordButton];
    [self.scrollView addSubview:self.tableView];
    [self.headView addSubview:self.drawerButton];
    [self.view addSubview:self.bottomView];
    [self.view addSubview:self.visuaView];
    [self.view addSubview:self.moreButton];
    [self.view addSubview:self.chartButton];
    [self.view addSubview:self.detailButton];
    
}
#pragma mark *** UITableViewDataSource ***
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSLog(@"%ld", _dataArray.count);
    return _dataArray.count;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    Home_cellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"kCell"];
    cell.markImageView.image = [UIImage imageNamed:_dataArray[indexPath.row][@"image"]];
    cell.tipsLabel.text = _dataArray[indexPath.row][@"dateidentify"];
    
    NSDate * date = [NSDate date];
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate:date];
    NSDate *localeDate = [date dateByAddingTimeInterval:interval];
    NSDateFormatter *dayformatter = [[NSDateFormatter alloc] init];
    NSDateFormatter *monthformatter = [[NSDateFormatter alloc] init];
    NSDateFormatter *yearformatter = [[NSDateFormatter alloc] init];
    [monthformatter setDateFormat:@"yyyy年MM月"];
    [dayformatter setDateFormat:@"yyyy-MM-dd"];
    [yearformatter setDateFormat:@"yyyy年"];
    
    NSString *daystring = [dayformatter stringFromDate:localeDate];
    NSString *monthString = [monthformatter stringFromDate:localeDate];
    NSString *yearString = [yearformatter stringFromDate:localeDate];
    if (indexPath.row == 0) {
        cell.describLabel.text = daystring;
    }else if (indexPath.row == 1){
        cell.describLabel.text = monthString;
    }else if (indexPath.row == 2){
        cell.describLabel.text = yearString;
    }
    
    return cell;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%ld", indexPath.row);
}
#pragma mark *** button_pressed ***

- (void)recordButtonTaped:(UIButton *)sender{
    ColletionViewController *collectionVC = [[ColletionViewController alloc]init];
//    [self presentViewController:collectionVC animated:YES completion:nil];
    self.navigationController.navigationBarHidden = NO;
    [self.navigationController pushViewController:collectionVC animated:YES];

}
- (void)moreButton_pressed:(UIButton *)sender{
    sender.selected = !sender.selected;
    if (sender.selected) {
        [UIView animateWithDuration:0.2 animations:^{
            self.moreButton.transform = CGAffineTransformRotate(self.moreButton.transform, M_PI_4);
            self.moreButton.center = CGPointMake(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2);
            _chartButton.alpha = 1;
            _detailButton.alpha = 1;
            self.chartButton.center = CGPointMake(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 + 70 * PROPORTION_H);
            self.detailButton.center = CGPointMake(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 + 140 * PROPORTION_H);
            self.scrollView.userInteractionEnabled = NO;
            self.visuaView.alpha = 1;
        }];
    }else{
        [UIView animateWithDuration:0.2 animations:^{
            self.moreButton.transform = CGAffineTransformRotate(self.moreButton.transform, - M_PI_4);
            _moreButton.center = CGPointMake(SCREEN_WIDTH / 2, SCREEN_HEIGHT - 45 * PROPORTION_H);
            _chartButton.center = _moreButton.center;
            _detailButton.center = _moreButton.center;
            _chartButton.alpha = 0;
            _detailButton.alpha = 0;
            self.scrollView.userInteractionEnabled = YES;
            self.visuaView.alpha = 0;
        }];
    }
    
    
}
#pragma mark *** Methods ***
-(void)openSideVcToEvent{
    [[ControllorManager shareManager].sideVc openAndCloseSideVcToEvent];
}

#pragma mark *** Getters ***
- (UIButton *)detailButton{
    if (_detailButton == nil) {
        _detailButton  = [UIButton buttonWithType:UIButtonTypeCustom];
        _detailButton.frame = _moreButton.frame;
        _detailButton.center = _moreButton.center;
        _detailButton.alpha = 0;
        _detailButton.backgroundColor = [UIColor orangeColor];
        _detailButton.layer.cornerRadius = 30 * PROPORTION_H;
        [_detailButton setImage:[UIImage imageNamed:@"account book"] forState:UIControlStateNormal];
    }
    return _detailButton;
}
- (UIButton *)chartButton{
    if (_chartButton == nil) {
        _chartButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _chartButton.frame = _moreButton.frame;
        _chartButton.center = _moreButton.center;
        _chartButton.alpha = 0;
        _chartButton.backgroundColor = [UIColor orangeColor];
        _chartButton.layer.cornerRadius = 30 * PROPORTION_H;
        [_chartButton setImage:[UIImage imageNamed:@"chart"] forState:UIControlStateNormal];
    }
    return _chartButton;
}
- (BottomView *)bottomView{
    if (_bottomView == nil) {
        _bottomView = [[BottomView alloc] initWithFrame:CRM(0, SCREEN_HEIGHT - 90 * PROPORTION_H, SCREEN_WIDTH, 90 * PROPORTION_H)];
        _bottomView.backgroundColor = [UIColor clearColor];
    }
    return _bottomView;
}
- (UIVisualEffectView *)visuaView{
    if (!_visuaView) {
        UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
        _visuaView = [[UIVisualEffectView alloc] initWithEffect:blur];
        _visuaView.frame = self.view.bounds;
        _visuaView.alpha = 0.0;
    }
    return _visuaView;
}
- (UIButton *)moreButton{
    if (_moreButton == nil) {
        _moreButton = [[UIButton alloc] init];
        _moreButton.frame = CRM(0, 0, 60 * PROPORTION_W, 60 * PROPORTION_H);
        _moreButton.center = CGPointMake(SCREEN_WIDTH / 2, SCREEN_HEIGHT - 45 * PROPORTION_H);
        [_moreButton setImage:[UIImage imageNamed:@"add"] forState:UIControlStateNormal];
        _moreButton.backgroundColor = [UIColor orangeColor];
        _moreButton.layer.cornerRadius = 30.0 * PROPORTION_W;
        [_moreButton addTarget:self action:@selector(moreButton_pressed:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _moreButton;
}
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CRM(0, CGRectGetMaxY(self.recordButton.frame) +  ruler / 3, SCREEN_WIDTH, 3 * ruler)];
        _tableView.backgroundColor = [UIColor colorWithRed:0.923 green:0.622 blue:1.000 alpha:1.000];
        _tableView.rowHeight = ruler;
        [_tableView registerClass:[Home_cellTableViewCell class] forCellReuseIdentifier:@"kCell"];
        
        _tableView.scrollEnabled = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor clearColor];
        [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
        [_tableView setSeparatorColor:[UIColor blackColor]];
    }
    return _tableView;
}
- (UIButton *)recordButton{
    if (!_recordButton) {
        
        _recordButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _recordButton.frame = CRM(0, 0, SCREEN_WIDTH - 60 * PROPORTION_W, ruler - 10 * PROPORTION_H);
        _recordButton.center = CPM(MID_X, CGRectGetMaxY(self.headView.frame) + 2 * ruler / 3 );
        _recordButton.layer.cornerRadius = 5.0;
        [_recordButton addTarget:self action:@selector(recordButtonTaped:) forControlEvents:UIControlEventTouchUpInside];
        [_recordButton setTitle:@"记一笔" forState:UIControlStateNormal];
        _recordButton.backgroundColor = [UIColor orangeColor];
    }
    return _recordButton;
}
- (HeadView *)headView{
    if (!_headView) {
        _headView = [[HeadView alloc] initWithFrame:CRM(0, 0, SCREEN_WIDTH, 270 * PROPORTION_H)];
        _headView.backgroundColor = [UIColor colorWithRed:1.000 green:0.597 blue:0.176 alpha:1.000];
        
    }
    return _headView;
}
- (UIScrollView *)scrollView{
    if (_scrollView == nil) {
        _scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
        _scrollView.scrollEnabled = YES;
        _scrollView.contentSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT * 1.005);
        
    }
    return _scrollView;
}
- (UIButton *)drawerButton{
    if (_drawerButton == nil) {
        _drawerButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _drawerButton.frame = CGRectMake(SCREEN_WIDTH - 60 * PROPORTION_W, 20, 60 * PROPORTION_W, 60 *PROPORTION_H);
        [_drawerButton setImage:[UIImage imageNamed:@"drawer"] forState:UIControlStateNormal];
        [_drawerButton addTarget:self action:@selector(openSideVcToEvent) forControlEvents:UIControlEventTouchUpInside];
    }
    return _drawerButton;
}



@end
